<?php

namespace Admin\Controller;

/**
 * 内容管理模块控制器类
 *
 * 后台内容管理相关操作
 *
 * @author: xuhui <xuh86@126.com>
 * @version: $Id$
 * @package: asoc.Admin.Controller
 *
 */


class ContentController extends CommonController {


    public function categoryList()
    {
        $act = $this->getString('act');
        if($act == 'ajax')
        {
            $cModel = D('Content');
            $categoryList = $cModel->getArticleNodeList(0,1);
            echo json_encode($categoryList);
        }
        else
        {
            $this->display();
        }
    }

    public function getCategoryNode()
    {
        $cModel = D('Content');
        $categoryList = $cModel->getArticleNodeList(0,1);
        echo json_encode($categoryList);
    }

    /**
     * 添加和编辑保存操作
     */
    public function categorySave()
    {
        $flag = 0;
        $cModel = M('Category');

        $id = $this->getInt('id');
        $text = $this->getString('text');
        $parent_id = $this->getInt('parent');

        if($id == 1)
        {
            echo 0;exit;
        }

        $data['node_name'] = $text;
        $data['parent_id'] = $parent_id;
        if($id == 0)
        {
            $cModel->add($data) && $flag = 1;
        }
        else
        {
            $flag = 1;
            $cModel->where("`node_id`={$id}")->save($data);
        }

        echo $flag;
    }


    /**
     * 删除分类
     */
    public function categoryDel()
    {
        $cModel = M('Category');
        $aModel = M('Article');
        $id = $this->getInt('id');
        $act = $this->getString('act');

        $subNum = $cModel->where("`parent_id`={$id}")->count();
        if($subNum > 0)
        {
            echo 2;exit;        //有子分类不能删除
        }
        $artilceNum = $aModel->where("`category_id`={$id}")->count();
        if($artilceNum > 0)
        {
            echo 3;exit;        //有文章不能删除
        }

        if($cModel->find($id))
        {
            $cModel->delete($id);
            echo 1;exit;
        }

        echo 0;
    }


    /**
     * 文章列表
     */
    public function articleList()
    {
        $act = $this->getString('act');
        if($act == 'ajax')
        {
            //查询条件
            $pageSize = $_POST['rows'];
            $pageNum = $_POST['page'];

            $category_id = $_POST['category_id'];
            $searchWord	=  $_POST['searchWord'];

            $whereStr = " 1=1 ";
            //组合查询条件
            if($category_id > 1 ) $whereStr .= " AND `category_id`={$category_id} ";
            if($searchWord != "")
            {
                $whereStr .= " AND  `title` LIKE '%".$searchWord."%' ";
            }

            $model = M('Article');

            //总条数
            $total = $model->where($whereStr)->count();
            $goodsList = $model->where($whereStr)->order("article_id DESC")->page($pageNum.','.$pageSize)->select();
            if($goodsList)
            {
                $cModel = M('Category');
                foreach($goodsList AS $key=>$val)
                {
                    $goodsList[$key]['pk_id'] = $val['article_id'];
                    $goodsList[$key]['category'] =	$cModel->where("`node_id`={$val['category_id']}")->getField('node_name');
                }
            }

            $this->ajaxBack(array('total'=>$total,'rows'=>$goodsList ? $goodsList :array()));
        }
        else
        {
            $this->display();
        }
    }


    public function articleSave()
    {
        $flag = 0;
        $aModel = M('Article');
        $article_id = $_POST['article_id'];
        $data['title'] = $_POST['title'];
        $data['category_id'] = $_POST['category_id'];
        $data['content'] = $_POST['content'];
		$data['show_index'] = isset($_POST['show_index']) ? 1 : 0;

        //上传封面图片
        if($_FILES['img']['name'] != '')
        {
            $rs = $this->uploadFile('./article/',2048);
            if($rs['status'] == 1)
            {
                $filename = $rs['info']['img']['savename'];
                $data['img'] = $filename;
            }
        }


        if($article_id > 0)
        {
            $flag = 1;
            $data['update_time'] = date('Y-m-d H:i:s');
            $aModel->where("`article_id`={$article_id}")->save($data);
        }
        else
        {
            $data['add_time'] = date('Y-m-d H:i:s');
            $aModel->add($data) && $flag = 1;
            //echo $aModel->getLastSql();
        }
        echo json_encode(array('status'=>$flag,'info'=>''));
    }

    public function articleDel()
    {
        $flag = 0;
        $aModel = M('Article');
        $id = $_POST['id'];
        if($aModel->find($id))
        {
            $flag = 1;
            $aModel->delete($id);
        }
        echo json_encode(array('status'=>$flag,'info'=>''));
    }

    public function setCateSort()
    {
        $flag = 0;
        $category_id = $this->getInt('cid');
        if($category_id > 0)
        {
            $cModel = M('Category');
            $info = $cModel->find($category_id);
            if($info)
            {
                $flag = 1;
                $max = $cModel->where("`parent_id`={$info['parent_id']}")->max('node_order');
                $cModel->where("`node_id`={$category_id}")->setField('node_order',$max+1);
            }
        }
        echo $flag;
    }
}